package org.dna.wickwtui.utils;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
import org.apache.wicket.util.lang.Classes;

/**
 * Contains the logic to check if any of the fields components passed into are
 * filled with some data.
 * 
 * Inspired by @see
 * org.apache.wicket.markup.html.form.validation.EqualInputValidator
 * */
public final class AtLeastOneValidator extends AbstractFormValidator {
	private static final long serialVersionUID = 1L;

	private FormComponent<?>[] m_formComponents;

	/**
	 * @param formComponents
	 *            , the list of components to check.
	 * */
	public AtLeastOneValidator(FormComponent<?>... formComponents) {
		m_formComponents = formComponents;
	}

	public FormComponent<?>[] getDependentFormComponents() {
		return m_formComponents;
	}

	public void validate(Form<?> form) {
		for (FormComponent<?> cf : m_formComponents) {
			if (!cf.getInput().isEmpty())
				return;
		}
		// all inputs are empty
		error(m_formComponents[0]);
	}

	@Override
	protected String resourceKey() {
		return Classes.simpleName(getClass());
	}
}
